#include "EvalBleedThroughSubstitution.hpp"


EvalBleedThroughSubstitution::EvalBleedThroughSubstitution(const girl::tools::Image &verso)
    : EvalBleedThroughAdd(verso)
{}



/** @todo mettre au propre */
float EvalBleedThroughSubstitution::evaluate(const girl::tools::Image &image)
{
    BleedThroughParameters *params = dynamic_cast<BleedThroughParameters * >(_params);
//    girl::tools::Image out(image);

    /*for(int i = 0; i< image.width(); i++)
    {
        for(int j = 0; j< image.height(); j++)
        {
            girl::tools::Pixel p = image.getPixel(i,j);
            out.setPixel(i,j,p);

        }
    }
*/
    int seuil = _thresholder.getRectoThreshold(image,getVerso(),_seuil);

 //   std::cout <<  " seuil " << seuil << std::endl;

    GlobalThresholdBinarisation rectoBinarisation(image,seuil);
    girl::regionDeque * rectoRegions = rectoBinarisation.underThdRegions();
    girl::toger * rectoToger = rectoBinarisation.toger();
   // std::cout << "Hello "<< std::endl;



//    std::cout << " recto SIZE of regions Under : " << rectoRegions->size() << std::endl;
//    std::cout << " recto SIZE of regions: " << rectoBinarisation.regions()->size() << std::endl;


    girl::regionDeque::iterator rectoIt = rectoRegions->begin();
    girl::regionDeque::iterator rectoItEnd = rectoRegions->end();


    int nb_sub =0;
    int areas = girl::tools::computeArea(*rectoToger,*rectoIt);
    int nbAreas = 1;

    for(; rectoIt != rectoItEnd; ++rectoIt)
    {

        //std::cout << *rectoIt << std::endl;
        bool in = false;
        girl::region r_recto = *rectoIt;
        girl::point p1;
        girl::point p2;
        girl::tools::computeBoundingBox(*rectoToger,r_recto,p1,p2);

        //std::cout << p1.x() << " " << p2.x()  << std::endl;

        girl::regionDeque::iterator versoIt = _versoRegions->begin();
        girl::regionDeque::iterator versoItEnd = _versoRegions->end();

        for(; versoIt != versoItEnd && !in; ++versoIt)
        {

            girl::region r_verso = *versoIt;

            std::vector<girl::point> points;
            _versoToger->regionDomain(r_verso,points);
            //  std::cout << "Nb Points on region " << r_recto << ": " << points.size() << std::endl;


            int currentArea = girl::tools::computeArea(*_versoToger,*versoIt);
            areas+= currentArea;
            nbAreas++;


            if(currentArea > 20 * (areas/nbAreas) /100){

                for(int i = 0; i<points.size() && !in;i++)
                {
                    in = (points[i].x() >= p1.x() && points[i].x() <= p2.x()
                          && points[i].y() >= p1.y() && points[i].y() <= p2.y());
                }
            }
            //std::cout << "in : " << in << std::endl;

        }
        if(in){
            nb_sub++;
            //std::vector<girl::point> points;
            //rectoToger->regionDomain(r_recto,points);
            //for(int i = 0; i < points.size(); i++)
             //   out.setPixel(points[i].x(),points[i].y(), girl::tools::Pixel(0,255,0));

        }

    }

    /*ImageConverter converter;
    QImage image1 = converter.convert(out);
    QImageWriter writer1("log.png", "png");
    writer1.write(image1);
    std::cout << "fin" << std::endl;
*/
    //ATTENTION DOCUMENTER le rectoregions
    float mesure = float(nb_sub) / float(rectoRegions->size());

    std::cout << "nb " << nb_sub << std::endl;
    std::cout << "NBSubEVAL " << mesure << std::endl;

    return mesure;
}
